package com.zhiteng.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhiteng.entity.AdjustBill;
import com.zhiteng.entity.LeaveBill;
import com.zhiteng.entity.Report;
import com.zhiteng.service.AdjustBillService;
import com.zhiteng.service.LeaveService;
import com.zhiteng.service.ReportServcie;
import com.zhiteng.utils.common.ExcleImpl;
import com.zhiteng.utils.common.ZTResult;
import com.zhiteng.vo.AdjustAndLeaveAndSub;
import com.zhiteng.vo.AdjustAndLeaveAndSubList;

@Controller
@RequestMapping("/report")
public class ReportController {

	@Autowired
	private AdjustBillService adjustBillService;

	@Autowired
	private LeaveService LeaveService;

	@Autowired
	private ReportServcie ReportServcie;

	/**
	 * 查询整个学校的报表
	 * 
	 * @param schoolId
	 * @return
	 */
	@RequestMapping("MakeReport")
	@ResponseBody
	public ZTResult<Report> MakeReport(@RequestParam("schoolId") String schoolId) {

		// 查询所有调课的数据
		List<AdjustBill> AdjustBillList = adjustBillService.queryAdjustBySchoolIdAndStatusAndType2(schoolId, 1, 1);
		Report report = new Report();
		if (AdjustBillList != null && !AdjustBillList.isEmpty()) {
			report.setAdjustNum(AdjustBillList.size());
			report.setPetitioner_adjustNum(AdjustBillList.size());
		}
		// 查询所有的代课数
		List<AdjustBill> AdjustBillList2 = adjustBillService.queryAdjustBySchoolIdAndStatusAndType2(schoolId, 1, 2);
		if (AdjustBillList2 != null && !AdjustBillList2.isEmpty()) {
			report.setSubstituteNum(AdjustBillList2.size());
			report.setPetitioner_substituteNum(AdjustBillList2.size());
		}

		// 查询请假的数量
		List<LeaveBill> LeaveBillList = LeaveService.queryLeaveBySchoolIdAndStatus(schoolId, 1);
		if (LeaveBillList != null && !LeaveBillList.isEmpty()) {
			report.setLeaveBillNum(LeaveBillList.size());
			Integer total = 0;
			for (LeaveBill leaveBill : LeaveBillList) {
				total = total + leaveBill.getTotal_course();
			}
			report.setTotal_course(total);
		}
		return ZTResult.result(0, "查询整个学校的报表成功", report);
	}

	@RequestMapping("MakeReportScreen")
	@ResponseBody
	public ZTResult<Set<AdjustAndLeaveAndSub>> MakeReportScreen(@RequestParam("schoolId") String schoolId,
			@RequestParam(value = "teacherId", required = false) String teacherId,
			@RequestParam(value = "subject", required = false) String subject,
			@RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime)
			throws java.text.ParseException {

		if (teacherId == null && subject == null) {
			return ZTResult.result(0, "老师id和科目必须有一个");
		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date startTime3 = sdf.parse(startTime);
		Date endTime3 = sdf.parse(endTime);

		Calendar calendar = new GregorianCalendar();
		calendar.setTime(endTime3);
		calendar.add(Calendar.DATE, 1);// 把日期往后增加一天.正数往后推,负数往前推
		endTime3 = calendar.getTime(); // 这个时间就是日期往后推一天的结果

		List<AdjustBill> AdjustBillList8 = null;

		List<AdjustBill> AdjustBillList10 = null;

		if (teacherId != null && subject == null) {
			// 调课
			AdjustBillList8 = ReportServcie.queryAdjustBySchoolIdAndStatusAndTypeAndQJAndTeacherId(schoolId, teacherId,
					1, 1, startTime3, endTime3);

			AdjustBillList10 = ReportServcie.queryAdjustBySchoolIdAndStatusAndTypeAndQJAndPetitionerId(schoolId,
					teacherId, 1, 1, startTime3, endTime3);

		}
		if (teacherId == null && subject != null) {
			// 调课
			AdjustBillList8 = ReportServcie.queryAdjustBySchoolIdAndStatusAndTypeAndQJAndSubject(schoolId, subject, 1,
					1, startTime3, endTime3);
		}

		Set<AdjustAndLeaveAndSub> list2 = new HashSet<AdjustAndLeaveAndSub>();

		if (AdjustBillList8 != null) {
			for (AdjustBill adjustBill : AdjustBillList8) {
				AdjustAndLeaveAndSub sub = new AdjustAndLeaveAndSub();
				sub.setTeacher_name(adjustBill.getTeacher_name());
				if (AdjustBillList8.size() == 0) {
					sub.setAdjustNum(null);
				} else {
					sub.setAdjustNum(AdjustBillList8.size());
				}

				list2.add(sub);
			}
		}

		if (AdjustBillList10 != null) {
			for (AdjustBill adjustBill : AdjustBillList10) {
				AdjustAndLeaveAndSub sub = new AdjustAndLeaveAndSub();
				sub.setTeacher_name(adjustBill.getPetitioner_name());
				if (AdjustBillList8.size() == 0) {
					sub.setAdjustNum(null);
				} else {
					sub.setAdjustNum(AdjustBillList8.size());
				}
				if (!list2.contains(sub)) {
					sub.setPetitioner_adjustNum(AdjustBillList10.size());
					list2.add(sub);
				} else {
					list2.remove(sub);
					sub.setPetitioner_adjustNum(AdjustBillList10.size());
					list2.add(sub);
				}
			}
		}

		List<AdjustBill> AdjustBillList9 = null;
		List<AdjustBill> AdjustBillList11 = null;
		if (teacherId != null && subject == null) {
			AdjustBillList9 = ReportServcie.queryAdjustBySchoolIdAndStatusAndTypeAndQJAndTeacherId(schoolId, teacherId,
					1, 2, startTime3, endTime3);

			AdjustBillList11 = ReportServcie.queryAdjustBySchoolIdAndStatusAndTypeAndQJAndPetitionerId(schoolId,
					teacherId, 1, 2, startTime3, endTime3);

		}
		if (teacherId == null && subject != null) {
			AdjustBillList9 = ReportServcie.queryAdjustBySchoolIdAndStatusAndTypeAndQJAndSubject(schoolId, subject, 1,
					2, startTime3, endTime3);
		}

		if (AdjustBillList9 != null) {
			for (AdjustBill adjustBill : AdjustBillList9) {
				AdjustAndLeaveAndSub sub = new AdjustAndLeaveAndSub();
				sub.setTeacher_name(adjustBill.getTeacher_name());
				if (AdjustBillList8.size() == 0) {
					sub.setAdjustNum(null);
				} else {
					sub.setAdjustNum(AdjustBillList8.size());
				}
				if (AdjustBillList10.size() == 0) {
					sub.setPetitioner_adjustNum(null);
				} else {
					sub.setPetitioner_adjustNum(AdjustBillList10.size());
				}

				if (!list2.contains(sub)) {
					sub.setSubstituteNum(AdjustBillList9.size());
					list2.add(sub);
				} else {
					list2.remove(sub);
					sub.setSubstituteNum(AdjustBillList9.size());
					list2.add(sub);
				}
			}
		}
		if (AdjustBillList11 != null) {
			for (AdjustBill adjustBill : AdjustBillList11) {
				AdjustAndLeaveAndSub sub = new AdjustAndLeaveAndSub();
				sub.setTeacher_name(adjustBill.getPetitioner_name());
				if (AdjustBillList8.size() == 0) {
					sub.setAdjustNum(null);
				} else {
					sub.setAdjustNum(AdjustBillList8.size());
				}
				if (AdjustBillList10.size() == 0) {
					sub.setPetitioner_adjustNum(null);
				} else {
					sub.setPetitioner_adjustNum(AdjustBillList10.size());
				}
				if (AdjustBillList9.size() == 0) {
					sub.setSubstituteNum(null);
				} else {
					sub.setSubstituteNum(AdjustBillList9.size());
				}

				if (!list2.contains(sub)) {
					sub.setPetitioner_substituteNum(AdjustBillList11.size());
					list2.add(sub);
				} else {
					list2.remove(sub);
					sub.setPetitioner_substituteNum(AdjustBillList11.size());
					list2.add(sub);
				}
			}
		}

		// 查询请假的数量
//		List<LeaveBill> LeaveBillList = LeaveService.queryLeaveBySchoolIdAndStatusAndQJ(schoolId, 1, startTime3,
//				endTime3);
		List<LeaveBill> LeaveBillList = null;
		if (teacherId != null && subject == null) {
			LeaveBillList = LeaveService.queryLeaveBySchoolIdAndStatusAndQJAndTeacherId(schoolId, teacherId, 1,
					startTime3, endTime3);
		}

		if (LeaveBillList != null) {
			for (LeaveBill leaveBill : LeaveBillList) {
				AdjustAndLeaveAndSub sub = new AdjustAndLeaveAndSub();

				sub.setTeacher_name(leaveBill.getTeacherName());
				if (AdjustBillList8.size() == 0) {
					sub.setAdjustNum(null);
				} else {
					sub.setAdjustNum(AdjustBillList8.size());
				}
				if (AdjustBillList10.size() == 0) {
					sub.setPetitioner_adjustNum(null);
				} else {
					sub.setPetitioner_adjustNum(AdjustBillList10.size());
				}
				if (AdjustBillList9.size() == 0) {
					sub.setSubstituteNum(null);
				} else {
					sub.setSubstituteNum(AdjustBillList9.size());
				}
				if (AdjustBillList11.size() == 0) {
					sub.setPetitioner_substituteNum(null);
				} else {
					sub.setPetitioner_substituteNum(AdjustBillList11.size());
				}

				if (!list2.contains(sub)) {
					list2.remove(sub);
					AdjustAndLeaveAndSub sub2 = new AdjustAndLeaveAndSub();
					sub2.setTeacher_name(leaveBill.getTeacherName());
					sub2.setTotal_course(leaveBill.getTotal_course());
					list2.add(sub2);
				} else {
					list2.remove(sub);
					sub.setTotal_course(leaveBill.getTotal_course());
					list2.add(sub);
				}
			}
		}
		return ZTResult.result(0, "查询整个学校的报表成功", list2);
	}

	ExcleImpl excleImpl = new ExcleImpl();

	@RequestMapping("downReport")
	@ResponseBody
	public ZTResult<Void> downReport(HttpServletResponse response, AdjustAndLeaveAndSubList AdjustAndLeaveAndSubList)
			throws ParseException {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		response.setContentType("application/binary;charset=UTF-8");
		try {
			ServletOutputStream out = response.getOutputStream();
			try {
				// 设置文件头：最后一个参数是设置下载文件名(这里我们叫：张三.pdf)
				response.setHeader("Content-Disposition",
						"attachment;fileName=" + URLEncoder.encode("老师表" + ".xls", "UTF-8"));
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}

			String[] titles = { "老师姓名", "调课数", "被调课数", "代课数", "被代课数", "请假的总课时" };

			excleImpl.export(titles, out, AdjustAndLeaveAndSubList.getAdjustAndLeaveAndSubList());
			return ZTResult.result(0, "查询整个学校的报表成功");
		} catch (Exception e) {
			e.printStackTrace();
			return ZTResult.result(1, "查询整个学校的报表失败");
		}
	}
}
